package com.example.s.utils;

import android.content.Context;
import android.net.Uri;

import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlProgressListener;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.transfer.COSXMLUploadTask;
import com.tencent.cos.xml.transfer.TransferConfig;
import com.tencent.cos.xml.transfer.TransferManager;
import com.tencent.cos.xml.transfer.TransferState;
import com.tencent.cos.xml.transfer.TransferStateListener;
import com.tencent.qcloud.core.auth.BasicLifecycleCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudLifecycleCredentials;
import com.tencent.qcloud.core.auth.SessionQCloudCredentials;
import com.tencent.qcloud.core.auth.ShortTimeCredentialProvider;
import com.tencent.qcloud.core.common.QCloudClientException;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

public class TCUpload {

    private CosXmlService cosXmlService;
    private Context context;

    public TCUpload(Context context) {
        this.context = context;
        String secretId = "AKIDTpzwgIk03xgCUdcOwGNMBmEWcT5av3ye"; //永久密钥 secretId
        String secretKey = "XrxpU4TebNxWwBCzYsyqUs9G8nCPOBl8"; //永久密钥 secretKey
        // keyDuration 为请求中的密钥有效期，单位为秒
        QCloudCredentialProvider myCredentialProvider =
                new ShortTimeCredentialProvider(secretId, secretKey, 300);


        String region = "ap-chengdu";
        // 创建 CosXmlServiceConfig 对象，根据需要修改默认的配置参数
        CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                .setRegion(region)
                .isHttps(false) // 使用 HTTPS 请求, 默认为 HTTP 请求
                .builder();
        // 初始化 COS Service，获取实例
        cosXmlService = new CosXmlService(context,
                serviceConfig, myCredentialProvider);
    }

    public interface OnUploadListener {
        void onSuccess(COSXMLUploadTask.COSXMLUploadTaskResult result);

        void onFail(CosXmlRequest request,
                    CosXmlClientException clientException,
                    CosXmlServiceException serviceException);
    }

    public void uploadUri(String name, Uri uri, OnUploadListener listener) {
        TransferConfig transferConfig = new TransferConfig.Builder().build();
        TransferManager transferManager = new TransferManager(cosXmlService,
                transferConfig);
        String bucket = "lilboss-space-1304435951"; //存储桶，格式：BucketName-APPID
//        String cosPath = name; //对象在存储桶中的位置标识符，即称对象键
        String uploadId = null;
        COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, System.currentTimeMillis() + name,
                uri, uploadId);
//设置返回结果回调
        cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
            @Override
            public void onSuccess(CosXmlRequest request, CosXmlResult result) {
                COSXMLUploadTask.COSXMLUploadTaskResult cOSXMLUploadTaskResult =
                        (COSXMLUploadTask.COSXMLUploadTaskResult) result;
                listener.onSuccess(cOSXMLUploadTaskResult);
            }

            @Override
            public void onFail(CosXmlRequest request,
                               CosXmlClientException clientException,
                               CosXmlServiceException serviceException) {
                listener.onFail(request, clientException, serviceException);
                if (clientException != null) {
                    clientException.printStackTrace();
                } else {
                    serviceException.printStackTrace();
                }
            }
        });
    }
}